package com.yb.web.goods;

import java.io.IOException;
import java.util.List;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.domain.goods.TgCategory;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;

public class CategoryAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -11843805088512180L;
	private TgCategory category;
	@SuppressWarnings("unchecked")
	private List list;
	private List<Object> rows;
	private int total;
	private int page;
	private CategoryService service;
	private Long id;
	private Long pid;
	private SystemLogService systemLogService;



	public CategoryAction() {
		service = new CategoryServiceImpl();
		systemLogService = new SystemLogServiceImpl();
	}

	public String showInfo() {
		try {
			category = service.findById(id);
		} catch (Exception e) {
			log.error("显示类别错误", e);
		}
		return "showInfo";
	}

	public String showChildInfo() {
		try {
			if (null != id)
				category = service.findById(id);
			if (null == category) {
				category = new TgCategory();
				category.setPid(pid.intValue());
			}
		} catch (Exception e) {
			log.error("显示类别错误", e);
		}
		return "showChildInfo";
	}

	/**
	 * 显示一级菜单
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String showList() {
		try {
			int limit = RequestUtil.getParam(request, "rows", 20);
			int count = service.getCount();
			total = count;
			list = service.findAll(page, limit);
			rows = list;
		} catch (Exception e) {
			log.error("显示第1级类别错误", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	/**
	 * 保存类别
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (null == category.getLevel())
				category.setLevel("0");
			service.saveCategory(category);
			 if(null==category.getId())
				 systemLogService.saveSystemLog(request, "行业类别", "新增类别--"+category.getName());
			 else
				 systemLogService.saveSystemLog(request, "行业类别", "修改类别--"+category.getName());
			if ("1".equals(category.getLevel())) {
				id = new Long(category.getPid());
				return "showChildren";
			}
		} catch (Exception e) {
			log.error("添加种类失败", e);
		}
		return "list";
	}

	/**
	 * 根据主键删除类别
	 * 
	 * @return
	 */
	public String delete() {
		try {
			int level = RequestUtil.getParam(request, "level", 2);
			if (service.checkDel(id, level)) {
				systemLogService.saveSystemLog(request, "行业类别", "删除类别--"+service.findById(id).getName());
				service.deleteById(id);
				response.getWriter().print("1");
			}else{
				response.getWriter().print("3");
			}
		} catch (Exception e) {
			log.error("", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 显示子分类
	 * 
	 * @return
	 */
	public String showChildren() {
		try {
			int limit = RequestUtil.getParam(request, "rows", 20);
			list = service.findByPid(id, page, limit);
			if (null != list && list.size() > 0)
				total = service.findByPid(id, -1, limit).size();
			rows = list;
		} catch (Exception e) {
			log.error("显示子分类出错", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	public String loadCategoryByPid() {
		try {
			list = service.findByPid(id, -1, -1);
			StringBuffer sb = new StringBuffer();
			sb.append(net.sf.json.JSONArray.fromObject(list));
			response.getWriter().print(sb.toString());
		} catch (Exception e) {
			log.error("加载子类别出错", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String loadCategory() {
		try {
			list = service.findAll(1, 50);
			;
			StringBuffer sb = new StringBuffer();
			sb.append(net.sf.json.JSONArray.fromObject(list));
			response.getWriter().print(sb.toString());
		} catch (Exception e) {
			log.error("加载子类别出错", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public TgCategory getCategory() {
		return category;
	}

	public void setCategory(TgCategory category) {
		this.category = category;
	}

	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}

	public List<Object> getRows() {
		return rows;
	}

	public void setRows(List<Object> rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

}
